<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin_controller
 *
 * @author hassanali
 */
class admin_controller extends APP_Controller {

    public $session_admin_data;
    public $admin_id;

    /**
     * 
     */
    function __construct() {
        parent::__construct();

        $this->session_admin_data = array(
            'admin_id' => $this->session->userdata('admin_ID'),
            'admin_name' => $this->session->userdata('admin_NAME'),
            'admin_email' => $this->session->userdata('admin_EMAIL'),
            'admin_password' => $this->session->userdata('admin_PASSWORD'),
            'admin_lastlogin' => $this->session->userdata('admin_LASTLOGIN'),
            'admin_mode' => $this->session->userdata('admin_MODE'),
            'admin_roles' => $this->session->userdata('admin_ROLES'),
            'admin_logged_in' => $this->session->userdata('admin_logged_in')
        );
        $this->admin_id = $this->session->userdata('admin_ID');
        //$this->manager_permsion();
        if (!empty($this->session_admin_data['admin_logged_in']) && uri_string() == ADMIN_FOLDER . '/dashboard/login') {
            redirect(ADMIN_FOLDER . '/dashboard');
        }
        $this->check_admin_login_information_not_change();
    }

    public function check_admin_login_information_not_change() {

        if (!empty($this->admin_id)) {
            $sql = $this->db->get_where('user', array('id' => $this->admin_id));
            if ($sql->num_rows()) {
                $row = $sql->row();
                if ($row->username != $this->session_admin_data['admin_name'] || $row->email != $this->session_admin_data['admin_email'] || $row->mode != $this->session_admin_data['admin_mode'] || $row->password != $this->session_admin_data['admin_password']) {
                    $this->destroy_session('admin');
                    $this->themes->set_message(array('url' => 'user/login', 'type' => 'success', 'message' => 'message_data_not_coorect_plz_try_again'));
                }
            } else {
                $this->destroy_session('admin');
                $this->themes->set_message(array('url' => 'user/login', 'type' => 'success', 'message' => 'message_data_not_coorect_plz_try_again'));
            }
        }
    }

    /**
     * 
     * @param type $url
     * @param type $total_rows
     * @param type $per_page
     * @param type $uri_segment
     * @return type
     */
    public function admin_pagination($url = 'index', $total_rows, $per_page, $uri_segment = 4) {

        $config['base_url'] = base_url() . ADMIN_FOLDER . '/' . get_segment(2) . '/' . $url;
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $per_page;
        $config['num_links'] = 5;
        $config['uri_segment'] = $uri_segment;

        $config['prev_link'] = lang('label_pagination_prev');
        $config['prev_tag_open'] = '<span class="btn white">';
        $config['prev_tag_close'] = '</span>';


        $config['next_link'] = lang('label_pagination_netx');
        $config['next_tag_open'] = '<span class="btn  white">';
        $config['next_tag_close'] = '</span>';


        $config['first_link'] = lang('label_pagination_first');
        $config['first_tag_open'] = '<span class="btn">';
        $config['first_tag_close'] = '</span>';


        $config['last_link'] = lang('label_pagination_last');
        $config['last_tag_open'] = '<span class="btn  white">';
        $config['last_tag_close'] = '</span>';


        $config['cur_tag_open'] = '<span class="btn btn-primary">';
        $config['cur_tag_close'] = '</span>';

        $config['num_tag_open'] = '<span class="btn btn-inverse" >';
        $config['num_tag_close'] = '</span>';

        $this->pagination->initialize($config);

        return $this->pagination->create_links();
    }

    function empty_id_actionall($id, $messge = 'message_plz_select_one_item') {
        if (empty($id)) {
            $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => $messge));
        }
    }

    public function manager_permsion() {
        $mode = $this->session_admin_data['admin_mode'];
        if ($mode == 'manager') {
            $controla = unserialize($this->session_admin_data['admin_roles']);


            $allow_permtion[] = 'dashboard';
            $allow_permtion[] = 'ajax';
            $allow_permtion[] = 'options/profile';
            $allow_permtion[] = 'user/login';
            $allow_permtion[] = 'user';
            $allow_permtion[] = 'user/logout';

            // if segmnet 2 in array allow to index url
            if (in_array($this->uri->segment(2), $controla)) {
                $allow_permtion[] = $this->uri->segment(2) . '/index';
            }
            if (in_array($this->uri->segment(2), $controla)) {
                $allow_permtion[] = $this->uri->segment(2) . '/view';
            }

            $controla = array_merge($controla, $allow_permtion);
            $url_path = $this->uri->segment(2) . '/' . $this->uri->segment(3);
            $url_path = rtrim($url_path, '/');


            if (!in_array($url_path, $controla) && !in_array($this->uri->segment(2), $allow_permtion)) {
                $this->themes->set_message(array('url' => 'dashboard', 'type' => 'error', 'message' => 'message_not_have_permison'));
            }
        }
    }

}

?>
